from django.db import models
from users.models import User, MembershipType

class Subscription(models.Model):
    """用户订阅模型"""
    STATUS_CHOICES = (
        ('active', '激活'),
        ('expired', '过期'),
        ('canceled', '已取消'),
        ('pending', '待处理'),
    )

    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='subscriptions', verbose_name='用户')
    membership_type = models.ForeignKey(MembershipType, on_delete=models.PROTECT, verbose_name='会员类型')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending', verbose_name='状态')
    start_date = models.DateTimeField(verbose_name='开始日期')
    end_date = models.DateTimeField(verbose_name='结束日期')
    auto_renew = models.BooleanField(default=True, verbose_name='自动续费')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        verbose_name = '订阅'
        verbose_name_plural = '订阅'
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.user.username} - {self.membership_type.name} - {self.get_status_display()}"

    def is_active(self):
        """检查订阅是否有效"""
        from django.utils import timezone
        now = timezone.now()
        return self.status == 'active' and self.start_date <= now <= self.end_date


class Payment(models.Model):
    """支付记录模型"""
    PAYMENT_METHOD_CHOICES = (
        ('alipay', '支付宝'),
        ('wechat', '微信支付'),
        ('manual', '手动处理'),
    )
    
    STATUS_CHOICES = (
        ('pending', '待支付'),
        ('success', '支付成功'),
        ('failed', '支付失败'),
        ('refunded', '已退款'),
    )

    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='payments', verbose_name='用户')
    subscription = models.ForeignKey(Subscription, on_delete=models.CASCADE, related_name='payments', verbose_name='关联订阅')
    amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='金额')
    payment_method = models.CharField(max_length=20, choices=PAYMENT_METHOD_CHOICES, verbose_name='支付方式')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending', verbose_name='状态')
    transaction_id = models.CharField(max_length=100, blank=True, null=True, verbose_name='交易ID')
    payment_time = models.DateTimeField(blank=True, null=True, verbose_name='支付时间')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        verbose_name = '支付记录'
        verbose_name_plural = '支付记录'
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.user.username} - {self.amount} - {self.get_status_display()}"


class PricingPlan(models.Model):
    """价格方案模型"""
    membership_type = models.OneToOneField(MembershipType, on_delete=models.CASCADE, related_name='pricing_plan', verbose_name='会员类型')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='价格')
    duration_days = models.IntegerField(default=30, verbose_name='有效天数')
    is_active = models.BooleanField(default=True, verbose_name='是否激活')
    description = models.TextField(blank=True, null=True, verbose_name='描述')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        verbose_name = '价格方案'
        verbose_name_plural = '价格方案'

    def __str__(self):
        return f"{self.membership_type.name} - {self.price}元/{self.duration_days}天"
